﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Linq.Expressions;
using System.Text;
using System.Data.Entity;
using padnug.Domain;
using PadnugFramework;
using PadnugRepositoryInterfaces;
namespace PadnugRepository
{
    public class PadnugMembership : IPadnugMembership
    {
        private padnugEntities db; 
        public PadnugMembership()
        {
            db = new padnugEntities();
        }
        #region Implementation of IPadnugMembership

        /// <summary>
        /// Creates a new user in the database
        /// </summary>
        /// <param name="padnugUser">instance of the PadnugUser class</param>
        public bool CreateUser(PadnugUser padnugUser)
        {
            bool isRegistered = false;
            try
            {
                var newUser = new User()
                                      {
                                          ActiveMember = padnugUser.ActiveMember,
                                          AmtPaid = padnugUser.AmtPaid,
                                          DatePaid = padnugUser.DatePaid,
                                          Email = padnugUser.Email,
                                          FirstName = padnugUser.FirstName,
                                          IPAddress = padnugUser.IPAddress,
                                          LastLogin = DateTime.UtcNow,
                                          LastName = padnugUser.LastName,
                                          Password = padnugUser.Password,
                                          Role = "User"
                                      };
                db.AddToUsers(newUser);
                db.AcceptAllChanges();
                isRegistered = true;
            }
            catch (Exception)
            {
                isRegistered = false;
            }
            return isRegistered;
        }

        /// <summary>
        /// Deletes a user from the PADNUG database
        /// </summary>
        /// <param name="username">user email address</param>
        /// <param name="deleteAllRelatedData">value is ignored</param>
        /// <returns></returns>
        public bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            bool isUserDeleted = false;
            try
            {
                var dbUser = db.Users.FirstOrDefault(
                                        u => u.Email.ToLower().CompareTo(username.ToLower()) == 0
                                    );
                db.DeleteObject(dbUser);
                db.AcceptAllChanges();
                isUserDeleted = true;
            }
            catch
            {
                
            }
            return isUserDeleted;
        }

        /// <summary>
        /// Gets PadnugUser from email and password
        /// </summary>
        /// <param name="username">email address</param>
        /// <param name="password">password</param>
        /// <returns></returns>
        public PadnugUser GetUser(string username, string password)
        {
            PadnugUser padnugUser = null;
            try
            {
                var dbUser = db.Users.FirstOrDefault(
                                u => u.Email.ToLower()
                                    .CompareTo(username.ToLower()) == 0 && u.Password.CompareTo(password) == 0);
                padnugUser = new PadnugUser()
                                 {
                                     ActiveMember = dbUser.ActiveMember,
                                     AmtPaid = dbUser.AmtPaid,
                                     DatePaid = dbUser.DatePaid,
                                     Email = dbUser.Email,
                                     FirstName = dbUser.FirstName,
                                     ID = dbUser.ID,
                                     IPAddress = dbUser.IPAddress,
                                     LastLogin = dbUser.LastLogin,
                                     LastName = dbUser.LastName,
                                     Password = dbUser.Password,
                                     Role = dbUser.Role
                                 };
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
            finally
            {
                if(padnugUser == null)
                {
                    padnugUser = new PadnugUser();
                }
            }
            return padnugUser;
        }

        /// <summary>
        /// Updates a PadNugUser object
        /// </summary>
        /// <param name="padnugUser">instance of the PadnugUser class</param>
        public void UpdateUser(PadnugUser padnugUser)
        {
            try
            {
                var dbUser = db.Users.FirstOrDefault(
                                        u => u.ID == padnugUser.ID
                                    );
                dbUser.ActiveMember = padnugUser.ActiveMember;
                dbUser.AmtPaid = padnugUser.AmtPaid;
                dbUser.DatePaid = padnugUser.DatePaid;
                dbUser.Email = padnugUser.Email;
                dbUser.FirstName = padnugUser.FirstName;
                dbUser.IPAddress = padnugUser.IPAddress;
                dbUser.LastLogin = DateTime.UtcNow;
                dbUser.LastName = padnugUser.LastName;
                dbUser.Password = padnugUser.Password;
                dbUser.Role = padnugUser.Role;
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }

        public bool ValidateUser(string username, string password)
        {
            bool isValidated = false;

            try
            {
                isValidated = db.Users.Any(u => u.Email.ToLower().CompareTo(username.ToLower()) == 0 && u.Password.CompareTo(password) == 0);
            }
            catch
            {

            }
            return isValidated;
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            bool? passwordUpdated = null;
            try
            {
                var padnugUser = GetUser(userName, oldPassword);
                padnugUser.Password = newPassword;
                UpdateUser(padnugUser);
                passwordUpdated = true;
            }
            catch (Exception)
            {
                passwordUpdated = false;
            }
            return passwordUpdated.Value;
        }

        #endregion
    }
}
